/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SystemManager.User.logic;
import Data.DAO.iUserStore;
import Data.DAO.IDataStore;
//import SystemManager.Login.Logic.Login_stub;
import data.pojo.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
/**
 *
 * @author Administrator
 */

 
/**
 *
 * @author Kite
 */
public class AddUserManage implements iAddUser{
    
    static private ArrayList<User> allUsers = new ArrayList<User>();
    private ArrayList<User> removeList = new ArrayList<User>();
    private ArrayList<User> serachList=new ArrayList<User>();
    private IDataStore UserDS = new iUserStore();
    private ArrayList<Integer> indexes = new ArrayList<Integer>();
    
    
    public AddUserManage (){
        allUsers = (ArrayList<User>)UserDS.getObjects();
        System.out.println("在构造函数里面，刷新 allUser");
    }
  
    private User b1 =new User();


    
    public void addOneUser(){
       
        allUsers.add(b1);
        UserDS.update(allUsers);
    }
    

    
    public Vector<List> searchTableVector(String key,String keywords) {
         return new Vector<List>(toUserTable(UserDS.searchObjectsByAttr(key, ".*" + keywords + ".*")));
    }
 
     public Vector<List> userTableVector() {       
         return new Vector<List>(toUserTable(allUsers));
    }  

    public Vector<Vector<String>> toUserTable(List list) {
    	try {
            Vector<User> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for(int i=0; i<seList.size(); i++){
                reList.add((Vector<String>)toUserStringArray(seList.get(i)));
            }
            return reList;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }     

    public List toUserStringArray(User user){
        Vector<String> list = new Vector<String>();
        list.add(user.getId()+"");//id
        //list.add("");//password
        list.add(user.getName());//name
        list.add(user.getStatus());//status
       
        return list;
    } 

    
    public List<User> getNewList() {
        return this.allUsers;
    }

    
    public boolean addToNewList(User user) {
        if(this.isValid(user)){
            this.allUsers.add(user); System.out.println("new user is " +user.getId()+"号");
            return true;
        }else{
            return false;
        }
    }
    
     private boolean isValid(User user){
        if(!this.UserDS.searchObjectsByAttr("id", String.valueOf(user.getId())).isEmpty()){
           return false;
        }
       
        
        for(User b: allUsers){
            if(b.getId().equals(user.getId()) ){
                System.out.println("已存在的id"+b.getId());
                return false;}
        }
        return true;
    }
     
     
     
     
    
    public User getByIndex(int index) {
         if(index!=-1)
            return this.allUsers.get(index);
         else return null ;
    }

    
    public boolean setByIndex(int index, User user) {
        if(this.isValid(user)){
            this.allUsers.set(index, user);
            return true;
        }else{
            return false;
        }
    }

   
    public void removeFromNewList(int index) {
        if((index!=-1)&&(index>allUsers.size()))
        this.allUsers.remove(index);
        else System.out.println(" 超出了"+index +"实际大小"+allUsers.size()+allUsers.get(allUsers.size()-1).getId());
    }


    public boolean commitNewList() {
        if(this.allUsers.isEmpty()){
            return false;
        }else{
            UserDS.update(allUsers);
           
            return true;
        }
    }
   private int  getIndex(User user){
         User b;int i;
        for( i =0;i<allUsers.size();i++){
             b = allUsers.get(i); //System.out.println("第"+i+"个 是"+b.getId()+"号");System.out.println("输入的id为"+user.getId());
             if(b.getId().equals(user.getId()) ){
                  return  i ;
                 }
    }             
             return -1 ;
   }
     // setStatus
    public boolean setStatus(int index,String status){
               User newuser ;
                 //System.out.println(""+user.getStatus());
              if((index!=-1)&&(index<allUsers.size())){
                  newuser =allUsers.get(index);
                  newuser.setStatus(status);
                 allUsers.set(index, newuser);
                 //(allUsers.get(index).getStatus());
                 return true ;
             }
       System.out.println("选中的是"+index );
        return false ;
    }
  // remove 
    public boolean Remove(int index){
         
                 
         if(index!=-1){
            this.allUsers.remove(index);
            return true ;
        }
         return false ;
    }
}


